$(function () {
    container.off("click");
    function getConsumableRepertoriesByName(page) {
        axios({
            url:url,
            params:{
                "async":true,
                "page":page,
                "consumableName":$("#searchName").val()
            }
        }).then(function (response) {
            $("#mainContainer").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    }
    //分页
    $.tbpage("#mainContainer",function (page) {
        getConsumableRepertoriesByName(page);
    });

    //搜索
    $("#searchNameBtn").click(function () {
        getConsumableRepertoriesByName(0);
    });

    // 获取添加耗材的界面
    $("#addConsumable").click(function() {
        $("#consumableDialogTitle").html("耗材入库");
        axios({
            method:'get',
            url: "/consumable/storage"
        }).then(function (response) {
            $("#consumableForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //获取耗材出库界面
    container.on("click","#outgoing",function () {
        $("#consumableDialogTitle").html("耗材出库");
        axios({
            method:'get',
            url:"/consumable/outgoing/"+$(this).attr('consumableId')
        }).then(function (response) {
            $("#consumableForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //提交耗材出库或入库，清空表单
    $("#consumableForm").submit(function() {
        var csrfToken = $("meta[name='_csrf']").attr("content");
        axios({
            method:"POST",
            url:"/consumable/outgoingOrStorage",
            data:$('#consumableForm').serialize(),
            headers:{'X-CSRF-TOKEN':csrfToken}
        }).then(function (response) {
            $('#consumableForm')[0].reset();
            if (response.data.success) {
                // 从新刷新主界面
                getConsumableRepertoriesByName(0);
                $("#consumableForm").html("");
                toastr.success(response.data.message);
            }else { toastr.error(response.data.message);}

        }).catch(function (error) {
            toastr.error("error!");
        });
        $("#consumableDialogClose").click();
        return false;
    });

    //关闭耗材Dialog界面
    $("#consumableDialogClose").click(function () {
        $("#consumableOutgoingForm").html("");
    });

});